//
//  CCNewPassWardViewController.m
//  cc
//
//  Created by ZLQ on 2016/10/25.
//  Copyright © 2016年 hbyj. All rights reserved.
//

#import "CCNewPassWardViewController.h"
#import "CCForgetPassWordViewController.h"
#import "CCKSZCViewController.h"
#import "SVProgressHUD.h"
#import "AFNetworking.h"
#import "NSString+Password.h"
#import "CCGeRenZhongXinController.h"

@interface CCNewPassWardViewController ()
{
    UIView *bgView;
    UITextField *passward;
}
@end

@implementation CCNewPassWardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"找回密码2/2";
    self.view.backgroundColor = [UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
    //添加返回按钮
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(clickaddBtn)];
    [addBtn setImage:[UIImage imageNamed:@"goback_back_orange_on"]];
    [addBtn setImageInsets:UIEdgeInsetsMake(0, -15, 0, 15)];
    addBtn.tintColor=[UIColor whiteColor];
    [self.navigationItem setLeftBarButtonItem:addBtn];
    
    [self createTextFields];
}

/**
 *  添加输入框
 */
-(void)createTextFields
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 75, self.view.frame.size.width-90, 30)];
    label.text = @"请设置新密码";
    label.textColor = [UIColor grayColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:label];
    
    CGRect frame = [UIScreen mainScreen].bounds;
    bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, frame.size.width-20, 50)];
    bgView.layer.cornerRadius = 3.0f;
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    passward = [self createTextFielfFrame:CGRectMake(100, 10, 200, 30) font:[UIFont systemFontOfSize:14] placeholder:@"6-20位字母或数字"];
    passward.clearButtonMode = UITextFieldViewModeWhileEditing;
    //密文形式
    passward.secureTextEntry = YES;
    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 50, 25)];
    phoneLabel.text = @"密码";
    phoneLabel.textColor = [UIColor blackColor];
    phoneLabel.textAlignment = NSTextAlignmentLeft;
    phoneLabel.font = [UIFont systemFontOfSize:14];
    
    UIButton *landBtn = [self createButtonFrame:CGRectMake(10, bgView.frame.size.height+bgView.frame.origin.y+30, self.view.frame.size.width-20, 37) backImageName:nil title:@"完成" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17] target:self action:@selector(okClick)];
    landBtn.backgroundColor = [UIColor colorWithRed:248/255.0f green:144/255.0f blue:24/255.0f alpha:1];
    landBtn.layer.cornerRadius = 5.0f;
    
    
    [bgView addSubview:passward];
    [bgView addSubview:phoneLabel];
    [self.view addSubview:landBtn];
}

-(UITextField *)createTextFielfFrame:(CGRect)frame font:(UIFont *)font placeholder:(NSString *)placeholder
{
    UITextField *textField=[[UITextField alloc]initWithFrame:frame];
    
    textField.font=font;
    
    textField.textColor=[UIColor grayColor];
    
    textField.borderStyle=UITextBorderStyleNone;
    
    textField.placeholder=placeholder;
    
    return textField;
}

-(UIImageView *)createImageViewFrame:(CGRect)frame imageName:(NSString *)imageName color:(UIColor *)color
{
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:frame];
    
    if (imageName)
    {
        imageView.image=[UIImage imageNamed:imageName];
    }
    if (color)
    {
        imageView.backgroundColor=color;
    }
    
    return imageView;
}

-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=frame;
    if (imageName)
    {
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    
    if (font)
    {
        btn.titleLabel.font=font;
    }
    
    if (title)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    if (color)
    {
        [btn setTitleColor:color forState:UIControlStateNormal];
    }
    if (target&&action)
    {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    return btn;
}

/**
 *  点击完成按钮
 */
-(void)okClick
{
    if ([passward.text isEqualToString:@""]) {
        [SVProgressHUD showInfoWithStatus:@"亲，密码不能为空"];
        return;
    }else if (passward.text.length<6 || passward.text.length>20){
        [SVProgressHUD showInfoWithStatus:@"亲，密码长度不正确，请检查密码长度"];
        return;
    }
    
    NSLog(@"===========%@",self.userPhone);
    //MD5加密
    NSString *pwd = [passward.text myMD5];
    NSDictionary *parmar = @{@"phone":self.userPhone,@"pwd":pwd};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://text.dawenkeji.com/Validate.asmx/UpPwd" parameters:parmar progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"上传成功%@",responseObject);
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        
//        [self.navigationController pushViewController:[[CCGeRenZhongXinController alloc] init] animated:YES];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传失败%@",error);
    }];
}

/**
 *  点击返回按钮
 */
-(void)clickaddBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
